package com.song.util;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.*;
import com.qcloud.cos.region.Region;

import java.io.File;

//文件上传工具类
public class FileUpload {

    public  synchronized void upload(File localFile,String name){
        // 1 初始化用户身份信息（secretId, secretKey）。
        String secretId = "AKIDTlT1982eFesxuJPq01jJk184pSw6d1jr";
        String secretKey = "KUkqZAe9EAvkPyN7SlXAe8RhOxeEeXQE";
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
// 2 设置 bucket 的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
// clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region("ap-chengdu");
        ClientConfig clientConfig = new ClientConfig(region);
// 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);

// 指定要上传到的存储桶
        String bucketName = "songtell-1251684550";
// 指定要上传到 COS 上对象键
        String key = "traffic/"+name;
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
    }

    public static void main(String[] args) {
        FileUpload fileUpload = new FileUpload();
        fileUpload.upload(new File("E:\\数据\\1767821837\\FileRecv\\MobileFile\\123.jpg"),"");
    }
}
